* Tariffs tau_it_S - industry level tariffs
* Updated March 3 moxnes@gmail.com

* Dependencies: This file requires running
* firm_patents2.do -> generates patentsfam2.dta
* weights_wic3.do -> Generates weightswic3.dta


cd /users/andreas/dropbox/work/patstat/data
global tmp "/tmp"

* ----------------------------
* Preliminaries
* ----------------------------
use TRAINS/allMFN_NACEr2_interpl, clear
drop isic4code
drop if year<1992 | year>2009
drop if tariff==.
bys iso2 nace2code: gen nyear=[_N]
keep if nyear>=18
* CH tariff is always 0 - drop
drop if iso2=="CH"
save $tmp/ttmp, replace

* Aggregate to 3 digit
gen nace2_1 = int(nace2code*10)/10
collapse (mean) tariff*, by(iso2 nace2_1 year)
reshape wide tariff, i( iso2 nace2_1 ) j( year )
ren iso2 appln_auth
tostring nace, gen(tmp) format(%7.2f) force  // Drop 2 digit
drop if substr(tmp,4,2)=="00"
drop tmp
save $tmp/tariff3dig, replace

* Add in 2 digit 
use $tmp/ttmp, clear
gen nace2_1 = int(nace2code)
collapse (mean) tariff*, by(iso2 nace2_1 year)
reshape wide tariff, i( iso2 nace2_1 ) j( year )
ren iso2 appln_auth
append using $tmp/tariff3dig
save $tmp/tariff, replace

* EU tariff
use $tmp/tariff, clear
keep if appln_auth=="DE"
replace appln_auth="EP"
save $tmp/tmpeu, replace

* Add back
use $tmp/tariff, clear
append using $tmp/tmpeu
save $tmp/tariff, replace

* Average tariff
use $tmp/tariff, clear
collapse (mean) tariff*, by(appln)
gen nace2_1 = 99
save $tmp/tmpnace, replace

use $tmp/tariff, clear
drop if nace2_1==99
append using $tmp/tmpnace
save tariffreshape, replace

* Tariff for home country
use tariffreshape, clear
ren tariff* tauthq*
ren appln headq
save $tmp/tariffreshapeHQ, replace

use $tmp/tmpnace, clear
ren tariff* tauthq*
ren appln headq
save $tmp/tmpnaceHQ, replace

* ----------------------------------------------
* RTA: Add RTA_ij = RTA_ji and RTA_ii
* ----------------------------------------------

use "CEPII_gravity/col_regfile09", clear
keep if year>=1985
keep year iso_o iso_d rta
save  $tmp/rta_ij, replace
rename iso_o iso_dest
rename iso_d iso_o
rename iso_dest iso_d
rename rta rta2
save  $tmp/rta_ji, replace
use $tmp/rta_ij, clear
merge 1:1 iso_o iso_d year using  $tmp/rta_ji

* Check if there is conflict between RTA_ij and RTA_ji
count if rta==0 & rta2==1 //ok
count if rta2==0 & rta==1 //ok
replace rta=rta2 if rta==. & rta2~=.
drop rta2 _merge
save $tmp/tmprta1, replace

* Add ii pairs
use $tmp/tmprta1, clear
contract iso_o year
drop _freq
gen iso_d=iso_o
gen byte rta=0
save $tmp/rta_ii, replace

use $tmp/tmprta1, clear
append using $tmp/rta_ii
save rta_missing_update, replace

* ----------------------------------------------
* Input missing data and convert to iso2
* ----------------------------------------------
use rta_missing_update, clear
* Assume that if no RTA in t, then no RTA in t-1
egen pair = group(iso_o iso_d)
tsset pair year
tsfill, full
gsort pair -year
by pair: replace rta=0 if rta==. & rta[_n-1]==0
by pair: replace iso_o=iso_o[_n-1] if iso_o==""
by pair: replace iso_d=iso_d[_n-1] if iso_d==""

* Assume that if RTA in t, then RTA in t+1
sort pair year
by pair: replace rta=1 if rta==. & rta[_n-1]==1
by pair: replace iso_o=iso_o[_n-1] if iso_o==""
by pair: replace iso_d=iso_d[_n-1] if iso_d==""

* We drop remaining pairs with missing data
keep if year>=1992
egen years = count(rta), by(pair)
drop if years<15
drop pair years
reshape wide rta, i( iso_o iso_d ) j( year )
save $tmp/tmprta2, replace

* Convert to ISO2
/* use tls801, clear */
use ../../Patstat_Spring2015/tls801/tls801, clear
replace iso_alpha3=trim(iso_alpha3)
replace ctry_code=trim(ctry_code)
drop if iso_alpha3==""
ren ctry_code iso2
ren iso_alpha3 iso3
keep iso2 iso3
drop if iso3=="DDR" & iso2=="DL"
replace iso3="ROM" if iso3=="ROU"
replace iso3="ZAR" if iso3=="COD"
save $tmp/iso, replace

clear  // http://www.statoids.com/wab.html
set obs 9
gen iso2 = "GP" in 1
gen iso3 = "GLP" in 1
replace iso2 = "GF" in 2
replace iso3 = "GUF" in 2
replace iso2 = "MQ" in 3
replace iso3 = "MTQ" in 3
replace iso2 = "NC" in 4
replace iso3 = "NCL" in 4
replace iso2 = "PF" in 5
replace iso3 = "PYF" in 5
replace iso2 = "RE" in 6
replace iso3 = "REU" in 6
replace iso2 = "PM" in 7
replace iso3 = "SPM" in 7
*replace iso2 = "AG" in 8
*replace iso3 = "ANT" in 8
replace iso2 = "FM" in 8
replace iso3 = "FSM" in 8
replace iso2 = "MH" in 9
replace iso3 = "MHL" in 9
append using $tmp/iso
save $tmp/iso, replace 


use $tmp/tmprta2, clear  // Note: We lose a few countries when converting from iso3 to iso2
ren iso_d iso3
merge m:1 iso3 using $tmp/iso, keep(match)
ren iso3 iso_d
ren iso2 appln_auth
drop _merge
ren iso_o iso3
merge m:1 iso3 using $tmp/iso, keep(match)
ren iso3 iso_o
ren iso2 headq
drop _merge iso*
order headq appln_auth 
sort headq appln_auth
save $tmp/tmprta3, replace

* Add EP as a country
use $tmp/tmprta3, clear
keep if headq=="FR"
replace headq="EP"
replace appln_auth="EP" if appln_auth=="FR"
save $tmp/tmpEP, replace

use $tmp/tmprta3, clear
keep if appln_auth=="FR"
replace appln_auth="EP"
save $tmp/tmpEP2, replace

use $tmp/tmprta3, clear
append using $tmp/tmpEP
append using $tmp/tmpEP2
save $tmp/rta, replace
save rta, replace


* ----------------------------------------------
* Restrict sample to firms with at least 1 patent in the post period
* ----------------------------------------------
use patentsfam3, clear
keep if y>=1992
keep if headq~=""
contract hrm
keep hrm
save $tmp/sample, replace

* ----------------------------------------------
* Merge tafiff data and weight wic
* ----------------------------------------------
*use weightswic5_to1985, clear 				// Baseline
*use weightswic5_to1975, clear  			// Placebo
use weightswic5_to1985_predicted, clear 	// Predicted weights

capture drop p tot
drop if headq==""

* Merge tariff for destination country
*replace nace=99 if nace==.  // Use these obs??
merge m:1 appln nace2_1 using tariffreshape, keep(match master)
drop _merge

* Mark missing tariffs and set them to zero for every year
count
gen MissTar = tariff1992==.
foreach vv of varlist tariff???? {
  replace `vv' = 0 if `vv'==.
}

* Merge RTA variable
merge m:1 headq appln using $tmp/rta, keep(match master)
drop _merge

* If missing RTA, set it to zero
gen Missrta = rta1992==.
foreach vv of varlist rta???? {
  replace `vv' = 0 if `vv'==.
}


* Merge tariff for home country
merge m:1 headq nace2_1 using $tmp/tariffreshapeHQ, keep(match master)

* If missing tariff, set it to zero for every year
gen MissTarhq = tauthq1992==.
foreach vv of varlist tauthq???? {
  replace `vv' = 0 if `vv'==.
}

save $tmp/tmp, replace

use $tmp/tmp, clear
gen homecty = appln_auth==headq 
gen homew = w if homecty==1

* Export weights
gen wEx = w
replace wEx = 0 if homecty==1
egen tmp = sum(wEx), by(hrm)
*replace wEx = wEx/tmp
gen wEx2 = wEx/tmp
drop tmp

*gen Missing = MissTar | MissTarhq | Missrta
gen Missing = MissTar

* Assume bilateral tariff is zero if RTA
forvalues i=1992/2006 {
  gen tariffApplied`i' =  tariff`i'*(~rta`i')
}

forvalues i=1992/2009 {
  gen taut`i' = tariff`i'*wEx 
  gen tautEx`i' = tariff`i'*wEx2
} 

forvalues i=1992/2006 {
  gen tautApp`i' = tariffApplied`i'*wEx
} 

*collapse (sum) taut???? tautEx???? Miss* (count) ncty=w (max) homecty homew, by(hrm headq nace tauthq????)
collapse (sum) taut???? tautApp???? Miss* (count) ncty=w (max) homecty homew, by(hrm headq nace tauthq????)
replace homew = 0 if homecty==0

*save tauT5d, replace
*save tauT5d_to1975, replace
save tauT5d_pred, replace


* ----------------------------------------------
* Reshape weights (to be used in estimation4.do)
* ----------------------------------------------
use $tmp/tmp, clear
egen tmp1 = sum(MissTar), by(hrm)
drop if tmp1>0 
keep hrm appln_auth w
reshape wide w, i(hrm) j(appln) string
foreach v of varlist w?? {
  replace `v'=0 if `v'==.
}
*save weightswic5_reshape_EU15_to1985, replace
save weightswic5d_reshape_to1985, replace


